package com.qilu.pe.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.dao.bean.Crowd;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.ActiviceCollector;
import com.qilu.pe.support.util.PeUtil;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ScreenUtils;
import com.ruitu.arad.util.SizeUtils;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class CrowdListActivity extends Base2Activity {
    private RecyclerView rcv_list;// 人群
    private TextView tv_ok;//

    private int[] imgs = {R.mipmap.ai_crowd_list_1, R.mipmap.ai_crowd_list_2, R.mipmap.ai_crowd_list_3
            , R.mipmap.ai_crowd_list_4, R.mipmap.ai_crowd_list_5, R.mipmap.ai_crowd_list_6};
    private CrowdListAdapter adapter;
    private List<Crowd> crowdList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_crowd_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiviceCollector.addActivity(this);
        rcv_list = findViewById(R.id.rcv_list);
        tv_ok = findViewById(R.id.tv_ok);

        setHeadTitle("选择人群");

        for (int i = 0; i < 6; i++) {
            crowdList.add(new Crowd());
        }

        GridLayoutManager manager = new GridLayoutManager(this, 3);
        adapter = new CrowdListAdapter(this);
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);
        adapter.setDataList(crowdList);

        setOnClickListener(tv_ok);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_ok) {
            int index = -1;
            for (int i = 0; i < crowdList.size(); i++) {
                if (crowdList.get(i).isSelect()) {
                    index = i;
                    break;
                }
            }
            if (index >= 0) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("crowd", PeUtil.getCrowdByIndex(index));
                if (index == 5) {
                    bundle.putInt("adapt", 0);
                } else {
                    bundle.putInt("adapt", index + 1);
                }
                startActivity(LifeHabitActivity.class, bundle);
            } else {
                ToastUtils.showShort("请选择人群");
            }
        }
    }

    private Disposable reqCrowdList() {
        return APIRetrofit.getDefault().reqCrowdList("hehe", "hehe")
                .compose(RxUtil.<BaseResult2<List>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List>>() {
                    @Override
                    public void accept(BaseResult2<List> listBaseResult2) throws Exception {
                        hideProgress();
                        if (listBaseResult2.isSuccess()) {

                        } else {

                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private class CrowdListAdapter extends ListBaseAdapter<Crowd> {

        public CrowdListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_crowd_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, final int position) {
            RoundedImageView iv_img = holder.getView(R.id.iv_img);
            CheckBox cb_check = holder.getView(R.id.cb_check);
            RelativeLayout rl_item = holder.getView(R.id.rl_item);

            int size = (ScreenUtils.getScreenWidth() - SizeUtils.dp2px(48)) / 3;
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rl_item.getLayoutParams();
            params.width = size;
            params.height = size;
            rl_item.setLayoutParams(params);

            Crowd crowd = mDataList.get(position);
            iv_img.setImageResource(imgs[position]);
            if (crowd.isSelect()) {
                cb_check.setChecked(true);
            } else {
                cb_check.setChecked(false);
            }

            rl_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < crowdList.size(); i++) {
                        if (i == position) {
                            crowdList.get(i).setSelect(true);
                        } else {
                            crowdList.get(i).setSelect(false);
                        }
                    }
                    adapter.setDataList(crowdList);
                }
            });
        }
    }
}